package com.tiger.smartapp;

import java.io.File;
import java.util.List;
import java.util.Map;

import net.youmi.android.AdListener;
import net.youmi.android.AdManager;
import net.youmi.android.AdView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ApklistActivity extends BaseActivity implements AdListener {
	static {
		// 应用Id 应用密码 广告请求间隔(s) 测试模式 应用版本号
		AdManager.init("8f7a2c6a9d881a58", "1bc7f6051671cfcb", 31, false, 2.1);
	}
	private CommonUtil util;
	private Button uninstall,install;
	private ProgressDialog pd_downloading;
	private ListView listView; 
	private List<Map<String, String>> mData;    
	private MyAdapter adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.apklist);
        listView = (ListView)this.findViewById(R.id.app_list);
        adapter = new MyAdapter(this);   
        listView.setAdapter(adapter);   
        listView.setOnItemClickListener(mLeftListOnItemClick);   
        pd_downloading = ProgressDialog.show(this, null,"正在读取本地安装包", true);
        uninstall=(Button)this.findViewById(R.id.xiezai);
        install=(Button)this.findViewById(R.id.anzhuang);
        uninstall.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ApklistActivity.this.startActivity(new Intent(ApklistActivity.this,MainActivity.class));
			}
        });
        install.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				adapter.notifyDataSetChanged();
			}
        });
        new Thread(new loadThread()).start();
        
        CommonUtil.setAdView(this, this);
    }
    AdapterView.OnItemClickListener mLeftListOnItemClick = new AdapterView.OnItemClickListener() {   
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3) {   
        	ViewHolder holder = (ViewHolder) view.getTag();  
        	if(holder!=null){
        		CommonUtil.install(new File(""+holder.path),ApklistActivity.this);
        	}
        }   
	};
	/**  
     * listview中点击按键弹出对话框  
     */  
    public final class ViewHolder {   
        public TextView apkname;   
        public String path;          
    }   
    class MyListView extends ListView{

		public MyListView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}
    	
    }
	 class MyAdapter extends BaseAdapter {   
		 
        private LayoutInflater mInflater;   
        public MyAdapter(Context context) {   
            this.mInflater = LayoutInflater.from(context);   
        }   
        public int getCount() {   
            // TODO Auto-generated method stub   
        	if(mData==null)
        		return 0;
            return mData.size();   
        }   
        public Object getItem(int arg0) {   
            // TODO Auto-generated method stub   
        	if(mData==null)
        		return null;
            return mData.get(arg0);   
        }   
        public long getItemId(int arg0) {   
            // TODO Auto-generated method stub   
            return arg0;   
        }   
        
		
		public View getView(int position, View convertView, ViewGroup parent) {   
            ViewHolder holder = null;   
            if (convertView == null) {   
                holder = new ViewHolder();   
                convertView = mInflater.inflate(R.layout.apkitem, null);   
                holder.apkname = (TextView) convertView.findViewById(R.id.titleleftlist);   
//                holder.path = (TextView) convertView.findViewById(R.id.infoleftlist);   
                   
                 
            } else {   
                holder = (ViewHolder) convertView.getTag();   
            }   
               
            if(mData!=null){
            holder.apkname.setText((String) mData.get(position).get("apkName"));   
            holder.path= mData.get(position).get("apkPath");             
            }
            convertView.setTag(holder);           
            return convertView;   
        }   
    }   
	 Handler handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case CommonUtil.status.DO:
			        if(adapter!=null){
			        	adapter.notifyDataSetChanged();
			        }
					break;
				}
			}
		};
	 private class loadThread implements Runnable{

			@Override
			public void run() {
				util=new CommonUtil("/sdcard/","apk");
		        mData=util.getMyApk();
		        if(mData!=null&&mData.size()>0){
		        	handler.sendEmptyMessage(CommonUtil.status.DO);
		        	if(pd_downloading!=null){
						pd_downloading.dismiss();
					}
		        }
			}
			
		}
	@Override
	public void onConnectFailed(AdView arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onReceiveAd(AdView arg0) {
		// TODO Auto-generated method stub
		
	};
}